#include "diskscanner.h"
#include "mainwindow.h"
#include "collection.h"
#include <QSettings>
#include <QMessageBox>

DiskScanner::DiskScanner()
{
    m_movieSuffixes << "*.mkv"
                    << "*.avi"
                    << "*.mpg"
                    << "*.wmv";

}

void DiskScanner::run()
{
    QSettings settings;
    QString path = settings.value("collection/path").toString();
    if (path.isEmpty()) {
        QMessageBox::critical(0, "Not configured", "There is no path to the collection defined.\nPlease go to the settings and configure one");
        return;
    }

    QStringList pathsToCheck(path);
    QStringList pathsChecked(path);

    while (!pathsToCheck.isEmpty()) {
        QDir directory(pathsToCheck.takeFirst());
        qDebug() << "scanning dir: " << directory.canonicalPath();
        pathsChecked.append(path);

        directory.setNameFilters(m_movieSuffixes);
        directory.setFilter(QDir::Files);
        if (directory.count() > 0) {
            // This is a folder with a video in
            scanVideo(directory);
        } else {
            // This is a folder with other folders only
            directory.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot);
            qWarning() << directory.entryInfoList().size();
            foreach (QFileInfo dir, directory.entryInfoList()) {
                if (!pathsChecked.contains(dir.canonicalFilePath()))
                    pathsToCheck.append(dir.canonicalFilePath());
            }
        }
    };

    deleteLater();
}

void DiskScanner::scanVideo(QDir dir)
{
    qDebug() << "scanning video: " << dir.canonicalPath();
    QStringList files;
    QStringList filenames;
    bool someRegistered = false;

    foreach (QFileInfo file, dir.entryInfoList()) {
        if (!Collection::fileIsRegistered(file.canonicalPath())) {
            files.append(file.canonicalPath());
            filenames.append(file.fileName());
        } else
            someRegistered = true;
    }

    // All files are already known
    if (files.isEmpty()) return;

    QString name = dir.dirName(); //TODO: stupid assumption (dirname is videoname);

    if (someRegistered) { // Just add the new files to the existing video
        foreach(QString file, files)
            Collection::addFile(file, name);

        return;
    }

    // Try to read stored tags
    QStringList tags;
    if (dir.exists("Tags.txt")) {
        QFile file(dir.absoluteFilePath("Tags.txt"));
        if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            while (!file.atEnd()) tags.append(file.readLine());
        }
    }

    // Try to see if we can get a cover
    QString cover;
    QStringList covers = dir.entryList(QStringList() << "*cover*.jpg" << "*cover*.png", QDir::Files, QDir::Name);
    if (!cover.isEmpty())
        cover = covers.at(0); //TODO

    // Try to see if there's any thumbnails available
    QStringList thumbnails = dir.entryList(QStringList() << "*jpg" << "*png", QDir::Files, QDir::Name);
    if (thumbnails.count() < files.count()) {
        //TODO generate thumbnails;
    } else { // Make sure the thumbnails and files are in the same order, hackishly
        thumbnails.clear();
        foreach(QString file, filenames) {
            QStringList thumb = dir.entryList(QStringList("*" + file + "*"), QDir::Files, QDir::Name);
            if (thumb.count() != 1) break;
            thumbnails.append(thumb.at(0));
        }
    }

    qDebug() << Q_FUNC_INFO << ": Adding new video; " << name
            << ", with cover; " << cover << ", with files; " << files << ", with thumbnails; " << thumbnails;

    Collection::addVideo(name, cover, dir.canonicalPath(), tags, files, thumbnails);
    qDebug() << "added";
}
